module utils.obj_mapping;

// /** 
//  * 对象映射工具类
//  */
// class ObjMapping
// {
//     import std.traits;

//     /** 
//      * 对象映射转换
//      * Params:
//      *   U = 目标类型
//      *   T = 源类型
//      *   srcObj = T 源类型实例
//      * Returns: 目标类型 U 实例
//      */
//     public static U mappingToObject (U, T)(T srcObj)
//             if (is(T == class) && is(U == class) && is(typeof(new U())))
//     {
//         U newObj = new U();
//         static if (FieldNameTuple!T.length == 0)
//         {
//             return newObj;
//         }

//         foreach (targetMemberName; FieldNameTuple!U)
//         {
//             foreach (sourceMemberName; FieldNameTuple!T)
//             {
//                 static if (targetMemberName == sourceMemberName)
//                 {
//                     __traits(getMember, newObj, targetMemberName) = __traits(getMember, srcObj, sourceMemberName);
//                 }
//             }
//         }

//         return newObj;
//     }
// }

import std.traits;

/** 
* 对象映射转换
* Params:
*   U = 目标类型
*   T = 源类型
*   srcObj = T 源类型实例
* Returns: 目标类型 U 实例
*/
public static U mappingToObject (U, T)(T srcObj) if (is(T == struct) && is(U == struct))
{
    U newObj;
    static if (FieldNameTuple!T.length == 0)
    {
        return newObj;
    }

    foreach (targetMemberName; FieldNameTuple!U)
    {
        foreach (sourceMemberName; FieldNameTuple!T)
        {
            static if (targetMemberName == sourceMemberName)
            {
                __traits(getMember, newObj, targetMemberName) = __traits(getMember, srcObj, sourceMemberName);
            }
        }
    }

    return newObj;
}
